package com.youlu.campus.web.platform;

import com.youlu.campus.entity.PlatformConfig;
import com.youlu.campus.entity.PointPlatformTaskConfigQueryVO;
import com.youlu.campus.entity.SystemApolloConfig;
import com.youlu.campus.entity.VO.res.PlatformSubConfigRes;
import com.youlu.campus.entity.platform.PlatformTaskInfo;
import com.youlu.campus.entity.platform.PlatformTaskRequest;
import com.youlu.campus.service.platform.PlatformConfigService;
import com.youlu.campus.service.platform.PointPlatformConfigService;
import com.youlu.campus.service.platform.PointPlatformTaskConfigService;
import com.youlu.campus.service.system.SystemApolloConfigService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 平台配置
 */
@Slf4j
@RestController
public class PlatformConfigController {
    @Autowired
    private PlatformConfigService platformService;

    @Autowired
    private PointPlatformConfigService pointPlatformConfigService;

    @Autowired
    private PointPlatformTaskConfigService pointPlatformTaskConfigService;
    @Autowired
    private SystemApolloConfigService systemApolloConfigService;

    /**
     * 平台配置
     *
     * @return
     */
    @GetMapping("/mini-app/platform-config")
    public QResult getPlatformConfig() {
        return QResult.success(platformService.find());
    }

    /**
     * 平台配置
     *
     * @return
     */
    @GetMapping("/public/platform-config/version")
    public QResult getFrontVersion() {
        PlatformConfig config = platformService.find();
        return QResult.success(config.getVersion());
    }

    @GetMapping("/mini-app/get/platform-info")
    public QResult getPlatformInfo() {
        return QResult.success(pointPlatformConfigService.findByAppId(RequestContext.getCurUser(), RequestContext.getAppId()));
    }

    @GetMapping("/mini-app/platform/sub-task")
    public QResult getPlatformSubTask() {
//        Config config = ConfigService.getAppConfig();
//        String cnf = config.getProperty("platform.sub.config", "{}");
//        log.info("获取平台配置订阅信息->{}", cnf);
        PlatformSubConfigRes platformSubConfig = new PlatformSubConfigRes();
//        if (StringUtils.isBlank(cnf)) {
//            return QResult.success(platformSubConfig);
//        }
//        PlatformSubConfigRes platformSubConfigJSON = JSON.parseObject(cnf, PlatformSubConfigRes.class);
        SystemApolloConfig systemApolloConfig = systemApolloConfigService.find();
        PlatformSubConfigRes platformSubConfigJSON=systemApolloConfig.getPlatformSubConfigRes();
        if (Objects.nonNull(platformSubConfigJSON)) {
            if (!platformSubConfigJSON.getAppId().equals(RequestContext.getAppId())) {
                return QResult.success(platformSubConfig);
            }
            if (platformSubConfigJSON.getSubOpen()) {
                platformSubConfig.setSubOpen(Boolean.TRUE);
            }
        }
        PointPlatformTaskConfigQueryVO pointPlatformTaskConfigQueryVO = new PointPlatformTaskConfigQueryVO();
        pointPlatformTaskConfigQueryVO.setPlatform(RequestContext.getCurPlatform());
        pointPlatformTaskConfigQueryVO.setAppId(RequestContext.getAppId());
        pointPlatformTaskConfigQueryVO.setStatus(1);
        PlatformTaskInfo tmplList = pointPlatformTaskConfigService.findTmplByAppId(pointPlatformTaskConfigQueryVO);
        if (Objects.nonNull(tmplList)) {
            platformSubConfig.setTemplateIds(StringUtils.join(tmplList.getTaskExtra(), ','));
            return QResult.success(platformSubConfig);
        }
        return QResult.success(platformSubConfig);
    }

    @PostMapping("/mini-app/platform/task/temps")
    public QResult getPlatformSubTask(@RequestBody PlatformTaskRequest request) {
        PlatformSubConfigRes platformSubConfig = new PlatformSubConfigRes();
        String curPlatform = RequestContext.getCurPlatform();
        String appId = "";
        if ("h5".equals(curPlatform)) {
            appId = request.getAppId();
        } else {
            appId = RequestContext.getAppId();
        }
        PointPlatformTaskConfigQueryVO pointPlatformTaskConfigQueryVO = new PointPlatformTaskConfigQueryVO();
        pointPlatformTaskConfigQueryVO.setPlatform(curPlatform);
        pointPlatformTaskConfigQueryVO.setAppId(appId);
        pointPlatformTaskConfigQueryVO.setTaskBizType(request.getTaskBizType());
        PlatformTaskInfo tmplList = pointPlatformTaskConfigService.findTmplByAppId(pointPlatformTaskConfigQueryVO);
        if (Objects.nonNull(tmplList)) {
            platformSubConfig.setTemplateIds(StringUtils.join(tmplList.getTaskExtra(), ','));
            platformSubConfig.setSubOpen(tmplList.getOpenSub());
            return QResult.success(platformSubConfig);
        }
        return QResult.success(platformSubConfig);
    }

}
